package com.tcxr.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.tcxr.entity.Login;
import com.tcxr.entity.Theme;
import com.tcxr.framework.base.DaoBase;
import com.tcxr.framework.system.SysParameters;

@Repository
public class ThemeDao extends DaoBase {

	/**
	 * 分页查询主题,时间分页。时间降序 排列，（最新时间靠前）
	 * 
	 * @param pageStart
	 *            起点
	 * @param number
	 *            分页出来的数量
	 * @param type
	 *  		帖子类型
	 *  @param status
	 *  		状态
	 * @return
	 */
	public List<Theme> getThemeDatePage(int pageStart, int number,int type,int status) {

		
		
		String sql="SELECT " +
				"th_id, " +
				"th_title, " +
				"th_content, " +
				"th_published_date, " +
				"th_up, " +
				"th_login_id, " +
				"th_type, " +
				"th_status" + 
				",login.lo_id"+
				",login.lo_name"+
				",login.lo_user"+
				",login.lo_img"+
				" FROM theme,login  where login.lo_id = theme.th_login_id and  th_status=? and th_type=? ORDER BY th_published_date DESC LIMIT ?, ?";
		
		
		
		List  rows = jdbcTemplate
				.queryForList(sql,
						new Object[] {status,type,pageStart,number});
		
		Iterator it = rows.iterator(); 
		List<Theme> list=new ArrayList<Theme>();
		Map userMap =null;
		Theme theme=null;
		Login login=null;
		while(it.hasNext()) {  
		      userMap = (Map) it.next();  
		      theme=new Theme();
		      theme.setThContent((String) userMap.get("th_content"));
		      theme.setThId((Integer) userMap.get("th_id")); 
		      theme.setThPublishedDate((Date) userMap.get("th_published_date")); 
		      theme.setThStatus((Integer) userMap.get("th_status")); 
		      theme.setThTitle((String) userMap.get("th_title")); 
		      theme.setThType((Integer) userMap.get("th_type")); 
		      theme.setThUp((Integer) userMap.get("th_up"));
		      login=new Login();
		      login.setLoId((Integer) userMap.get("lo_id"));
		      login.setLoName((String) userMap.get("lo_name"));
		      login.setLoUser((String) userMap.get("lo_user"));
		      login.setLoImg((String) userMap.get("lo_img"));
		      
		      theme.setLogin(login);
		      
		 
		      
		      list.add(theme);
			}
		
		
		

		return list;
	}
	

	/**
	 * .添加一条新的 主题内容
	 * @param theme
	 * @return 返回一个 新生成的ID
	 */
	public  int addTheme(Theme theme)
	{
		theme.setThId(super.getTableID("Th_Id", "theme"));
		
		String sql=  " INSERT INTO  theme ("+
					"th_id, "+
					"th_title, "+
					"th_content, "+
					"th_published_date, "+
					"th_up, "+
					"th_login_id, "+
					"th_type, "+
					"th_status"+
					") VALUES (?,?,?,?,?,?,?,?) ";
	
		
 jdbcTemplate.update(sql,new Object[]{
		 theme.getThId(),
		 theme.getThTitle(),
		 theme.getThContent(),
		// theme.getThPublishedDate(),
		 SysParameters.getDate(),
		 theme.getThUp(),
		 theme.getLogin().getLoId(),
		 theme.getThType(),
		 theme.getThStatus()});
 
		
		
		return theme.getThId();
	}
	
	
	
	
	
	
	
	
	
	
	
}
